<template>
  <view class="container">
    <view class="login-box">
      <image
        class="logo"
        src="/src/static/logo/logo2-02.png"
        mode="aspectFit"
      ></image>
      <text class="title">欢迎登录</text>

      <view class="agreement-box">
        <checkbox-group @change="handleAgreementChange">
          <label class="agreement-label">
            <checkbox :checked="isAgreed" color="#007AFF" />
            <text class="agreement-text">我已阅读并同意</text>
            <text class="agreement-link" @click="showAgreement"
              >《用户协议》</text
            >
          </label>
        </checkbox-group>
      </view>

      <button
        class="login-btn"
        :class="{ 'login-btn-disabled': !isAgreed }"
        @click="handleLogin"
      >
        微信一键登录
      </button>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref } from "vue";
import http from "@/utils/request";

const isAgreed = ref(false);

// 处理协议勾选
const handleAgreementChange = (e: any) => {
  isAgreed.value = e.detail.value.length > 0;
};

// 显示用户协议
const showAgreement = () => {
  uni.navigateTo({
    url: "/pages/agreement/index",
  });
};

// 处理登录
const handleLogin = () => {
  if (!isAgreed.value) {
    uni.showToast({
      title: "请先同意用户协议",
      icon: "none",
    });
    return;
  }

  uni.showLoading({
    title: "正在登录",
    mask: true,
  });

  // 使用getUserProfile替代getUserInfo
  uni.getUserProfile({
    desc: "用于完善用户资料",
    lang: "zh_CN",
    success: (result: any) => {
      if (result.errMsg === "getUserProfile:ok") {
        uni.login({
          success: async (res: any) => {
            try {
              const response = await http.post("/user/api/userInfo/code", {
                code: res.code,
                username: result.userInfo.nickName,
                avatar: result.userInfo.avatarUrl,
              });

              const userData = response.data;
              // 保存用户信息
              const userInfo = {
                id: userData.id || 0,
                username: userData.username || result.userInfo.nickName,
                avatar: userData.avatar || result.userInfo.avatarUrl,
                email: userData.email || "",
                phone: userData.phone || "",
                gender: userData.gender || "0",
              };

              // 保存token和用户信息到本地存储
              if (userData.token) {
                uni.setStorageSync("token", userData.token);
                uni.setStorageSync("userInfo", userInfo);

                uni.showToast({
                  title: "登录成功",
                  icon: "success",
                });

                // 返回上一页或首页
                const pages = getCurrentPages();
                if (pages.length > 1) {
                  uni.navigateBack();
                } else {
                  uni.switchTab({
                    url: "/pages/my/my",
                  });
                }
              }
            } catch (error) {
              console.error("登录失败：", error);
              uni.showToast({
                title: "登录失败，请重试",
                icon: "error",
              });
            }
          },
          fail: (err) => {
            console.error("微信登录失败：", err);
            uni.showToast({
              title: "微信授权失败，请重试",
              icon: "error",
            });
          },
        });
      }
    },
    fail: (err) => {
      console.error("获取用户信息失败：", err);
      uni.showToast({
        title: "获取用户信息失败，请重试",
        icon: "error",
      });
    },
  });
};
</script>

<style>
.container {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #e0f7fa 0%, #bbdefb 100%); /* 渐变背景 */
  background-size: 400% 400%; /* 扩大背景尺寸以支持动画 */
  animation: gradientAnimation 15s ease infinite; /* 添加动画 */
  padding: 20px;
}

/* 定义渐变动画 */
@keyframes gradientAnimation {
  0% {
    background-position: 0% 50%;
  }
  50% {
    background-position: 100% 50%;
  }
  100% {
    background-position: 0% 50%;
  }
}

.login-box {
  width: 100%;
  max-width: 360px;
  background-color: #ffffff;
  border-radius: 18px;
  padding: 40px 30px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.15);
  display: flex;
  flex-direction: column;
  align-items: center;
  position: relative;
  overflow: hidden;
}

.login-box::before {
  content: "";
  position: absolute;
  top: -50px;
  left: -50px;
  width: 150px;
  height: 150px;
  background-color: rgba(0, 122, 255, 0.1);
  border-radius: 50%;
  filter: blur(30px);
  z-index: 0;
}

.login-box::after {
  content: "";
  position: absolute;
  bottom: -50px;
  right: -50px;
  width: 120px;
  height: 120px;
  background-color: rgba(255, 69, 0, 0.1);
  border-radius: 50%;
  filter: blur(30px);
  z-index: 0;
}

.logo {
  width: 250px;
  height: 200px;
  z-index: 1;
}

.title {
  font-size: 26px;
  font-weight: 600;
  color: #333;
  margin-bottom: 35px;
  z-index: 1;
}

.agreement-box {
  width: 100%;
  margin-bottom: 35px;
  z-index: 1;
}

.agreement-label {
  display: flex;
  align-items: center;
  font-size: 13px;
  color: #555;
}

.agreement-text {
  margin-left: 6px;
}

.agreement-link {
  color: #007aff;
  text-decoration: underline;
  margin-left: 4px;
}

.login-btn {
  width: 100%;
  height: 48px;
  background-color: #007aff;
  color: #ffffff;
  border-radius: 24px;
  font-size: 17px;
  font-weight: 500;
  display: flex;
  align-items: center;
  justify-content: center;
  border: none;
  box-shadow: 0 4px 10px rgba(0, 122, 255, 0.3);
  transition: all 0.3s ease;
  z-index: 1;
}

.login-btn:active {
  transform: translateY(1px);
  box-shadow: 0 2px 8px rgba(0, 122, 255, 0.3);
}

.login-btn-disabled {
  background-color: #cccccc;
  color: #ffffff;
  box-shadow: none;
}
</style>
